package com.viphuli.fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.viphuli.app.R;
import com.viphuli.common.Constants;
import com.viphuli.http.ApiRequest;

/**
 * 企业预约
 * 
 * @author li.li
 *
 */
public class HomeCompanyOrderFragment extends BaseProgressFragment implements TextWatcher {

	@Bind(R.id.id_main_content_service_company_order_name)
	protected EditText orderName;

	@Bind(R.id.id_main_content_service_company_order_contact)
	protected EditText orderContacts;

	@Bind(R.id.id_main_content_service_company_order_phone)
	protected EditText orderPhone;

	@Bind(R.id.id_main_content_service_company_order_people_num_text)
	protected EditText orderPeopleNumText;

	@Bind(R.id.id_main_content_service_company_order_people_num_add)
	protected TextView orderPeopleNumAdd;

	@Bind(R.id.id_main_content_service_company_order_people_num_decrease)
	protected TextView orderPeopleNumDecrease;

	@Bind(R.id.id_main_content_service_company_order_time)
	protected EditText orderTime;

	@Bind(R.id.id_main_content_service_company_order_site)
	protected EditText orderSite;

	@Bind(R.id.id_main_content_service_company_order_remark)
	protected EditText orderRemark;

	@Bind(R.id.id_main_content_service_company_order_btn)
	protected TextView orderBtn;
	
	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home_company_order;
	}
	
	@Override
	protected void initParams() {
		
	}

	@Override
	protected void initView() {
		orderName.addTextChangedListener(this);
		orderContacts.addTextChangedListener(this);
		orderPhone.addTextChangedListener(this);
		orderTime.addTextChangedListener(this);
		orderSite.addTextChangedListener(this);
		orderRemark.addTextChangedListener(this);
		orderBtn.addTextChangedListener(this);
		orderPeopleNumText.addTextChangedListener(this);

		verifyFiled();
	}

	@Override
	protected void initData() {
	}

	@OnClick(R.id.id_main_content_service_company_order_people_num_add)
	public void peopleNumAdd() {

		String numStr = orderPeopleNumText.getText().toString();

		int num = Integer.valueOf(numStr);
		orderPeopleNumText.setText(String.valueOf(++num));

	}

	@OnClick(R.id.id_main_content_service_company_order_people_num_decrease)
	public void peopleNumDecrease() {

		String numStr = orderPeopleNumText.getText().toString();

		if (StringUtils.isNotBlank(numStr) && !"0".equals(numStr)) {

			int num = Integer.valueOf(numStr);
			orderPeopleNumText.setText(String.valueOf(--num));

		}
	}

	@OnClick(R.id.id_main_content_service_company_order_btn)
	public void submit() {

		String company = orderName.getText().toString();
		String contact = orderContacts.getText().toString();
		String tel = orderPhone.getText().toString();
		String number = orderPeopleNumText.getText().toString();
		String serviceTime = orderTime.getText().toString();
		String site = orderSite.getText().toString();
		String remark = orderRemark.getText().toString();

		if (StringUtils.isBlank(company)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_company), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(tel)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_contact), Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtils.isBlank(contact)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_phone_number), Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtils.isBlank(number) || Integer.parseInt(number) < Constants.ORDER_MIN_NUMBER_PERSON) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_number_person), Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtils.isBlank(serviceTime)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_service_time), Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtils.isBlank(site)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_site), Toast.LENGTH_SHORT).show();
			return;
		}
		if (StringUtils.isBlank(remark)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_remark), Toast.LENGTH_SHORT).show();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("company", company);
		params.put("contact", contact);
		params.put("tel", tel);
		params.put("number", number);
		params.put("service_time", serviceTime);
		params.put("site", site);
		params.put("remark", remark);

		ApiRequest.homeCompanyOrder.request(this, params);

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	@Override
	public void afterTextChanged(Editable s) {

		verifyNum();
		verifyFiled();
	}

	/**
	 * 验证订购数
	 */
	private void verifyNum() {
		String number = orderPeopleNumText.getText().toString();

		if (StringUtils.isBlank(number) || Integer.parseInt(number) <= Constants.ORDER_MIN_NUMBER_PERSON) {//下限

			orderPeopleNumDecrease.setBackgroundResource(R.drawable.bg_decrease_number_disable);
			//			Toast.makeText(getActivity(), getString(R.string.tip_order_number_min), Toast.LENGTH_SHORT).show();

			if (StringUtils.isBlank(number) || Integer.parseInt(number) < Constants.ORDER_MIN_NUMBER_PERSON)
				orderPeopleNumText.setText(String.valueOf(Constants.ORDER_MIN_NUMBER_PERSON));

		} else if (Integer.parseInt(number) >= Constants.ORDER_MAX_NUMBER) {//上限

			orderPeopleNumAdd.setBackgroundResource(R.drawable.bg_add_number_disabled);
			//			Toast.makeText(getActivity(), getString(R.string.tip_order_number_max), Toast.LENGTH_SHORT).show();

			if (Integer.parseInt(number) > Constants.ORDER_MAX_NUMBER)
				orderPeopleNumText.setText(String.valueOf(Constants.ORDER_MAX_NUMBER));

		} else {//可点击

			orderPeopleNumDecrease.setBackgroundResource(R.drawable.sl_decrease_number);
			orderPeopleNumAdd.setBackgroundResource(R.drawable.sl_add_number);

		}
	}

	/**
	 * 动态验证表单
	 */
	private void verifyFiled() {

		String company = orderName.getText().toString();
		String contact = orderContacts.getText().toString();
		String tel = orderPhone.getText().toString();
		String number = orderPeopleNumText.getText().toString();
		String serviceTime = orderTime.getText().toString();
		String site = orderSite.getText().toString();
		String remark = orderRemark.getText().toString();

		if (StringUtils.isBlank(company) //
				|| StringUtils.isBlank(tel) //
				|| StringUtils.isBlank(contact) //
				|| StringUtils.isBlank(number) || Integer.parseInt(number) < Constants.ORDER_MIN_NUMBER_PERSON //
				|| StringUtils.isBlank(serviceTime) //
				|| StringUtils.isBlank(site)//
				|| StringUtils.isBlank(remark)) {

			orderBtn.setBackgroundResource(R.drawable.bg_common_btn_blue_pressed);

		} else {
			orderBtn.setBackgroundResource(R.drawable.sl_common_btn_blue);
		}

	}
	
}
